package com.devops.admin.graphql;

import com.devops.admin.model.DOSExportDataJob;
import com.devops.admin.model.ExportDataJobBatch;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSExportDataJobLogPo;
import com.devops.admin.po.DOSExportDataJobPo;
import com.devops.admin.po.ExportDataJobBatchPo;
import com.devops.admin.service.DOSExportDataJobLogService;
import com.devops.admin.service.DOSExportDataJobService;
import com.devops.admin.service.ExportDataJobBatchService;
import com.devops.admin.vo.DOSExportDataJobLogVo;
import com.devops.admin.vo.DOSPageVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jxt
 * @Notes
 * @date 2023/12/25 16:50
 */
@DgsComponent
public class DOSExportDataJobFetcher {

    @Resource
    private DOSExportDataJobService dosExportDataJobService;

    @Resource
    private DOSExportDataJobLogService dosExportDataJobLogService;

    @Resource
    private ExportDataJobBatchService exportDataJobBatchService;

    @DgsMutation
    @DOSLog(description = "创建导出任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean createExportDataJob(@InputArgument DOSExportDataJob exportDataJob) {
        return dosExportDataJobService.checkAndCreate(exportDataJob);
    }

    @DgsMutation
    @DOSLog(description = "更新导出任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean updateExportDataJob(@InputArgument DOSExportDataJob exportDataJob) {
        return dosExportDataJobService.checkAndUpdate(exportDataJob);
    }

    @DgsMutation
    @DOSLog(description = "删除导出任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean deleteExportDataJob(@InputArgument Integer jobId) {
        return dosExportDataJobService.stopAndRemove(jobId);
    }

    @DgsMutation
    @DOSLog(description = "手动执行导出任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean executeExportDataJob(@InputArgument Integer jobId) {
        return dosExportDataJobService.execute(jobId);
    }

    @DgsMutation
    @DOSLog(description = "启动或暂停导出任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean startOrPauseExportDataJob(@InputArgument Integer jobId, @InputArgument Boolean active) {
        return dosExportDataJobService.startOrPauseExportDataJob(jobId, active);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public DOSExportDataJobPo getExportDataJobById(@InputArgument Integer jobId) {
        return dosExportDataJobService.getExportDataJobPoById(jobId);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public TableResult<DOSExportDataJobPo> getExportDataJobList(@InputArgument DOSExportDataJob exportDataJob,
                                                                @InputArgument DOSPageVo pageVo) {
        return dosExportDataJobService.getExportDataJobList(exportDataJob, pageVo);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public TableResult<DOSExportDataJobLogPo> getExportDataJobLogList(@InputArgument DOSExportDataJobLogVo exportDataJobLogVo,
                                                                      @InputArgument DOSPageVo pageVo) {
        return dosExportDataJobLogService.getExportDataJobLogList(exportDataJobLogVo, pageVo);
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public List<DOSExportDataJob> getAllExportDataJobs(Integer type) {
        return dosExportDataJobService.lambdaQuery().eq(type != null, DOSExportDataJob::getType, type).list();
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public TableResult<ExportDataJobBatchPo> getExportDataJobBatchList(@InputArgument ExportDataJobBatch exportDataJobBatch,
                                                                       @InputArgument DOSPageVo pageVo) {
        return exportDataJobBatchService.getExportDataJobBatchList(exportDataJobBatch, pageVo);
    }

    @DgsMutation
    @DOSLog(description = "创建批量导出任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean createExportDataJobBatch(@InputArgument ExportDataJobBatch exportDataJobBatch) {
        return exportDataJobBatchService.createBatch(exportDataJobBatch);
    }

    @DgsMutation
    @DOSLog(description = "更新批量任务列表")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean updateExportDataJobBatch(@InputArgument ExportDataJobBatch exportDataJobBatch) {
        return exportDataJobBatchService.updateBatch(exportDataJobBatch);
    }

    @DgsMutation
    @DOSLog(description = "删除批量任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean deleteExportDataJobBatch(@InputArgument Integer batchId) {
        return exportDataJobBatchService.deleteBatch(batchId);
    }

    @DgsMutation
    @DOSLog(description = "启动或暂停批量任务")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER})
    public Boolean startOrPauseExportDataJobBatch(@InputArgument Integer batchId, @InputArgument Boolean active) {
        return exportDataJobBatchService.startOrPauseExportDataJobBatch(batchId, active);
    }
}
